///*
// * To change this template, choose Tools | Templates
// * and open the template in the editor.
// */
//package neuralmusic;
//
//import java.awt.Rectangle;
//import java.awt.event.ActionEvent;
//import java.awt.event.ActionListener;
//import java.util.List;
//import java.util.logging.Level;
//import java.util.logging.Logger;
//import javax.sound.midi.InvalidMidiDataException;
//import javax.sound.midi.MidiDevice;
//import javax.sound.midi.MidiMessage;
//import javax.sound.midi.MidiUnavailableException;
//import javax.sound.midi.Receiver;
//import javax.sound.midi.ShortMessage;
//import javax.swing.JFrame;
//import javax.swing.Timer;
//
//import neuralmusic.brain.Module2D;
//import neuralmusic.brain.Connection;
//import neuralmusic.brain.TimeRef;
//import neuralmusic.brain.gui.NetworkDisplayPanel;
//import neuralmusic.brain.gui.NetworkMonitorPanel;
//import neuralmusic.midiIO.NoteAction;
//import neuralmusic.simplemidiplayer.SimpleMidiPlayer;
//
///**
// *
// * 
// * 8  label
// * 1
// * 
// * @author pjl
// */
//public class MainMidiPlayer {
//
//    static TimeRef ref;
//    private static List<Connection> inputs;
//    private static NetworkDisplayPanel brainPanel;
//    static SimpleMidiPlayer midiPlayer;
//    private static NetworkMonitorPanel monPanel;
//    private static Module2D brain;
//
//    public static void main(String[] args) throws MidiUnavailableException, InvalidMidiDataException, InterruptedException {
//        int nOut = 1;
//        int nInputs = 49;
////     MidiDevice out=MyMidiSystem.findMidiOut("Gervill");
////        out.open();
////        final Receiver r=out.getReceiver();
//
//        midiPlayer = SimpleMidiPlayer.createSimpleMidiPlayer(new Receiver() {
//
//            @Override
//            public void send(MidiMessage message, long timeStamp) {
//                try {
//                    if (message instanceof ShortMessage) {
//                        ShortMessage shm = (ShortMessage) message;
//                        if (shm.getChannel() == 9) {
//                            if (shm.getData2() != 0) {
//        //                        brain.train(0);
//                            }
//                        }
//                    }
//                    if (false) midiPlayer.send(message);
//                } catch (MidiUnavailableException ex) {
//                    Logger.getLogger(MainMidiPlayer.class.getName()).log(Level.SEVERE, null, ex);
//                }
//
//
//                if (message instanceof ShortMessage) {
//                    ShortMessage shm = (ShortMessage) message;
//                    int cmd = shm.getCommand();
//                    if (cmd == ShortMessage.NOTE_ON || cmd == ShortMessage.NOTE_OFF) {
//                        int pitch = shm.getData1();
//                        int vel = shm.getData2();
//                        //      System.out.println(pitch+" " + vel);
//                        pitch = pitch - 36;
//                        if (pitch >= inputs.size() || pitch < 0) {
//                            return;
//                        }
//                        inputs.get(pitch).fire(vel / 128.0f); //, timeStamp);
////                        brainPanel.repaint();
//                    }
//                }
//            }
//
//            @Override
//            public void close() {
//            }
//        }, "/home/pjl/MIDI");
//
//
//        ref = new TimeRef();
//
//
//        NoteAction[] noteActions = new NoteAction[nOut];
//
//        Receiver recv = new Receiver() {
//
//            @Override
//            public void send(MidiMessage message, long timeStamp) {
//                try {
//                    midiPlayer.send(message);
//                } catch (MidiUnavailableException ex) {
//                    Logger.getLogger(MainMidiPlayer.class.getName()).log(Level.SEVERE, null, ex);
//                }
//            }
//
//            @Override
//            public void close() {
//                throw new UnsupportedOperationException("Not supported yet.");
//            }
//        };
//
//        for (int i = 0; i < nOut; i++) {
//            noteActions[i] = new NoteAction(recv, i + 42, 9);
//        }
//
//
//    //    brain = new Brain(ref, noteActions, nInputs);
//  //      inputs = brain.getInputs();
//
//        // -----BRAIN DRAIN ---------------------------------------------------
////        Brain brain = new Brain(ref);
////
////        Point p1 = new Point(0, 0);
////        Point p2 = new Point(0, 128);
////
////        // Inputs
////
////        float thresh = 0.1f;
////        long delay =  0L;   // don't delay inputs
////        float outputVal = 10.0f;
////        float timeConstant = .001f;
////        int nInputs = 49;
////        inputs = brain.addInputs(nInputs, p1, p2, thresh, outputVal, timeConstant, delay);
////
////
////        // outputs
////        Point p3 = new Point(400, 0);
////        Point p4 = new Point(400, 128);
////
////        thresh = 0.1f;
////        delay =  0L;   // don't delay outputs
////        outputVal = 1.0f;
////        timeConstant = .01f;
////        int nOut = 12;
////        brain.addOutputs(nOut, p3, p4, thresh, outputVal, timeConstant, delay);
////
////
////        // Grey matter
////        Rectangle rect = new Rectangle(5, 5, 390, 118);
////
////        delay =  100L;
////        outputVal = 1.0f;
////        timeConstant = .01f;
////        thresh = .1f;
////        float internalConnectWeight = .2f;
////        int nInternal = 400;
////        int nConnect = 10;
////        brain.addGreyMatter(nInternal, nConnect, rect, thresh, outputVal, timeConstant, delay, internalConnectWeight);
////
////
////        // connect input to grey matter
////
////        float weightMax = 20;
////        nConnect = 30;
////
////        brain.connectInputsToGreyMatter(nConnect, weightMax);
////
////        weightMax = .1f;
////
////        brain.connectGreyMatterToOutputs(weightMax);
//
//        // --------------------------------------------
//
//        JFrame frame = new JFrame();
//        brainPanel = new NetworkDisplayPanel(brain);
//        frame.setContentPane(brainPanel);
//        frame.setSize(800, 600);
//        frame.setVisible(true);
//
//        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//
//
//        monPanel = new NetworkMonitorPanel(brain);
//        JFrame monitorFrame = new JFrame();
//        monitorFrame.setContentPane(monPanel);
//
//        monitorFrame.pack();
//        monitorFrame.setVisible(true);
//
//
//
//        Timer t = new Timer(50, new ActionListener() {
//
//            @Override
//            public void actionPerformed(ActionEvent e) {
//                brainPanel.repaint();
//            }
//        });
//        t.start();
//    }
//}
